//
//  Sports_ViewController.m
//  Bicycle
//
//  Created by dllo on 16/4/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Sports_ViewController.h"
#import "SportsRun_ViewController.h"
@interface Sports_ViewController ()<PassDelegate>
@property (weak, nonatomic) IBOutlet UIButton *StartBtn;
@property(nonatomic,assign)float Sum_distance;
@property(nonatomic,assign)NSInteger Sum_counts;
@property(nonatomic,strong)NSUserDefaults *user;
@property(nonatomic,assign)BOOL isStart;
@property(nonatomic,strong)SportsRun_ViewController *startRun;


@end

@implementation Sports_ViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"passBool" object:nil];
}

-(void)reciveNotice:(NSNotification *)notification{
    
    self.isStart = !(BOOL)[notification.userInfo objectForKey:@"start"];
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    if (self.isStart) {
        
        self.StartBtn.backgroundColor = [UIColor orangeColor];
        [self.StartBtn setTitle:@"返回运动" forState:(UIControlStateNormal)];
        
    }else{
        
        self.StartBtn.backgroundColor = [UIColor colorWithRed:56/255.0 green:192/255.0 blue:110/255.0 alpha:1];
        [self.StartBtn setTitle:@"开  始" forState:(UIControlStateNormal)];
        _startRun = nil;
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    self.view.backgroundColor = [UIColor orangeColor];
//    UIImageView *img = [[UIImageView alloc]initWithFrame:self.view.frame];
//    img.image = [UIImage imageNamed:@""];
//    img.backgroundColor = [UIColor redColor];
//    [self.view addSubview:img];
    
    self.navigationController.navigationBarHidden = YES;
    //创建用户基本数据
    self.user = [NSUserDefaults standardUserDefaults];
    //从数据中获取总距离
    self.distance.text = [NSString stringWithFormat:@"%.2f",[self.user floatForKey:@"Distance"]/1000];
    
    //    self.Sum_counts = 0;
    
    self.counts.text = [NSString stringWithFormat:@"%ld",(long)[self.user integerForKey:@"Counts"]];
    
    if (self.isStart) {
        
        self.StartBtn.backgroundColor = [UIColor orangeColor];
        [self.StartBtn setTitle:@"返回运动" forState:(UIControlStateNormal)];
        
        
    }

    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:)name:@"pass" object:nil];
    
}
- (IBAction)runStart:(UIButton *)sender {
    if (!self.startRun) {
        self.startRun = [[SportsRun_ViewController alloc]init];
        self.startRun.delegate = self;
    }
    self.startRun.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    if (!self.isStart) {
        sender.backgroundColor = [UIColor orangeColor];
        [sender setTitle:@"返回运动" forState:(UIControlStateNormal)];
        [self presentViewController:self.startRun animated:YES completion:nil];
        
        self.isStart = YES;
        self.startRun.isStart = self.isStart;//点击开始后,将bool值设为yes,传入startRunVC
    }else{
        
        [self presentViewController:self.startRun animated:YES completion:nil];
    }

    
}
-(void)tongzhi:(NSNotification *)shu{
    
    
    
    NSString *str= shu.userInfo[@"start"];
    
    NSLog(@"aaaaaaaa%@",str);
    
    float a = [str floatValue];
    
    //    里程数加入用户默认设置
    //
    
    self.Sum_distance = [self.user floatForKey:@"Distance"];
    self.Sum_distance +=a;
    NSLog(@"aaaaaaa%f",a);
    //    NSLog(@"bbbbbbbb%f",save.distance);
    
    //显示里程数
    self.distance.text = [NSString stringWithFormat:@"%.2f",self.Sum_distance / 1000];
    [self.user setFloat:self.Sum_distance forKey:@"Distance"];
    //判断 加入次数
    self.Sum_counts = [self.user integerForKey:@"Counts"];
    if (a >= 0.01) {
        self.Sum_counts++;
    }
    [self.user setFloat:self.Sum_counts forKey:@"Counts"];
    //显示次数
    self.counts.text = [NSString stringWithFormat:@"%ld",(long)self.Sum_counts];
   
}
-(void)passRunStart:(BOOL)isStart{
    
    self.isStart = isStart;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
